﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiXiu.Common;
using AiXiu.DAL;
using AiXiu.Model;

namespace AiXiu.BLL
{
    public class LoginBLL
    {
        public OperResult<TBUsers> Login(string UserName,string Password) 
        {
            //参数非空
            if (string.IsNullOrEmpty(UserName)||string.IsNullOrEmpty(Password))
            {
                return OperResult<TBUsers>.Failed("用户名密码不能为空！");
            }
            //通过用户名查询是否存在
            LoginDAL loginDAL = new LoginDAL();
            TBLogins login= loginDAL.getUserbyName(UserName);
            if (login==null)
            {
                return OperResult<TBUsers>.Failed("该用户不存在！");
            }
            //验证用户密码
            SHAEncryption sHA1 = new SHAEncryption();
            var pwd = sHA1.SHA1Encrypt(Password);
            if (login.Password!=pwd) 
            {
                return OperResult<TBUsers>.Failed("密码错误！");
            }
            //通过Id查找用户的具体信息(TbUsers)
            var id = login.Id;
            TBUsers users= loginDAL.get(id);
            return OperResult<TBUsers>.Succeed(users);
        }

        public OperResult<TBUsers> Phonein(string Phone, string Password) 
        {
            //参数非空
            if (string.IsNullOrEmpty(Phone) || string.IsNullOrEmpty(Password))
            {
                return OperResult<TBUsers>.Failed("手机号密码不能为空！");
            }
            //通过手机号查询是否存在
            LoginDAL loginDAL = new LoginDAL();
            TBLogins login = loginDAL.getUserbyPhone(Phone);
            if (login == null)
            {
                return OperResult<TBUsers>.Failed("该用户不存在！");
            }
            //验证用户密码
            SHAEncryption sHA1 = new SHAEncryption();
            var pwd = sHA1.SHA1Encrypt(Password);
            if (login.Password != pwd)
            {
                return OperResult<TBUsers>.Failed("密码错误！");
            }
            //通过Id查找用户的具体信息(TbUsers)
            var id = login.Id;
            TBUsers users = loginDAL.get(id);
            return OperResult<TBUsers>.Succeed(users);
        }
    }
}
